<?php 
/**
 * Controller de presupuesto 
 * @author Natanael
 * 
 */

class Presupuesto extends Controller{
	
	public function __construct()
	{
		parent::controller();
		$this->load->database();
		$this->load->model(array('mpresupuesto'));
	}
	
	public function index()
	{
	
		$this->load->view('main', array('content_page' => 'list_presupuesto','menutop'=>5));
	}
	
	public function add()
	{
		$this->load->view('main', array('content_page' => 'add_presupuesto','menutop'=>5));
	}
	
	public function edit($id)
	{
		$registroId = $this->mpresupuesto->loadRegistroById($id);
		$this->load->view('main', array('content_page' => 'edit_presupuesto','menutop'=>5,'registro'=>$registroId));
	}
	
	public function delete()
	{
		$registro = $this->input->post('delete_rows');
		if ($registro > 0) {
			if ($this->mpresupuesto->delete($registro) == true) {
			
			}
			
		}
	}
	
	public function save()
	{
		$id = @$this->input->post('id');
		
		$data = array(
		        'anos'         =>$this->input->post('anos'),
		        'wk'           =>$this->input->post('wk'),
		        'presupuesto'   =>$this->input->post('presupuesto')
		             );	
		
		if ($id > 0) {
			
		if ($this->mpresupuesto->editRegistro($data,$id) == true) {
			  	   redirect('presupuesto');
			  }
			
		}else{
			  if ($this->mpresupuesto->crearRegistro($data) == true) {
			  	   redirect('presupuesto');
			  }
			
			
		}
		
	}
	

	
	public function data()
	{
	    $page = $this->input->post('page') ? $this->input->post('page') : 1;
		$sortname = $this->input->post('sidx') ? $this->input->post('sidx') : $this->input->post('sidx');
		$sortorder = $this->input->post('sord') ? $this->input->post('sord') : 'desc';
		$limit = $this->input->post('rows') ? $this->input->post('rows') : 10;

		$start = $limit * $page - $limit;
		if($start < 0) $start = 0;

		header("Expires: Mon, 26 Jul 1997 05:00:00 GMT" );
		header("Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . "GMT" );
		header("Cache-Control: no-cache, must-revalidate" );
		header("Pragma: no-cache" );

        $records = $this->mpresupuesto->loadPresupuesto($start, $limit, $sortname, $sortorder);	
        
        $records = array_reverse($records);
        
        $totalRecords = count($records);
        
        $totalPages = ceil($totalRecords/$limit);
		if ($page > $totalPages) $page = $totalPages;

		$json_output = array(
		'page'		=> $page,
		'total'		=> $totalPages,
		'records'	=> $totalRecords,
		'rows'		=> array()
		);
		
		if ($totalRecords > 0) {
			
		    foreach ($records as $k)
		    {
		       $arr = $json_output['rows'][] = array(
				    'id'=>$k->getId(),
				    'anos'=>$k->getAnos(),
				    'wk' => $k->getWk(),
				    'presupuesto'=>$k->getPresupuesto()
		        );
		    }
			
		}
		
		$a=json_encode($json_output);
		$a=str_replace('\\','',$a);
		echo $a;
		
	}
}


?>